/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable"],
    function($,$bb,$datatable){
	var servers = {};
	var URL = $bb.baseUrl("followapp");
    
    /**
     * 入参
     */
    var inParams = function(args){
        var params = $bb.getPageParams();
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "busiType":params.busiType,
                "showFlag":"1",
                "inoculationCard":$.trim($('#inoculationCard').val()),
                "userName": $.trim($('#userName').val()),
                "phoneNo": $.trim($('#phoneNo').val()),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnGender = function(e){
        return e.gender == "F" ? "女" : "男";
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":'inoculationCard'},
            {"data":'userName'},
            {"data":columnGender},
            {"data":'phoneNo'},
            {"data":'birthday'},
            {"data":'nodeName'},
            {"data":'parentName'},
            {
                "data":null,
                "className":"progress-viccine"
            },
            {
                "data":null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 进度
        var h1 = "";
        if( data.nodeStatusArr && $.isArray(data.nodeStatusArr) && data.nodeStatusArr.length > 0 ){
            var len = data.nodeStatusArr.length;
            var width = parseInt(100 / len) + '%';
            for (var i = 0; i < len; i++) {
                var status = data.nodeStatusArr[i];
                var obj = {};
                if( status == '0' ){                   // 未开始
                    obj.className = "progress-notstart";
                    obj.title = "未开始";
                }else if( status == '1' ){             // 已完成
                    obj.className = "progress-finish";
                    obj.title = "已完成";
                }else if( status == '2' ){             // 过期（未开始）
                    obj.className = "progress-overdue";
                    obj.title = "过期";
                }else if( status == '3' ){             // 过期（补填）
                    obj.className = "progress-fill";
                    obj.title = "补填";
                }else if( status == '4' ){             // 未完成（部分任务填写）
                    obj.className = "progress-nofinish";
                    obj.title = "未完成";
                }
                h1 += '<div style="width:'+width+'"><i class="'+obj.className+'" data-toggle="tooltip" data-placement="top" title="'+obj.title+'"></i></div>';
            }
        }
        var html = '<div class="progress-wrap">'+ h1 +'</div>';
        $('td.progress-viccine', row).html(html);

        // 操作
        //var h2 = '<div class="operate-box"><i class="fa fa-search color-mint" data-toggle="tooltip" data-placement="top" title="查看"></i><i class="fa fa-pencil-square-o color-blue ml8" data-toggle="tooltip" data-placement="top" title="编辑"></i></div>';
        var h2 = '<div class="operate-box"><i class="fa fa-pencil-square-o color-blue" data-toggle="tooltip" data-placement="top" title="编辑"></i></div>';
        $('td.operate', row).html(h2);
        /*$("td.operate .operate-box i.color-mint",row).unbind("click");
        $("td.operate .operate-box i.color-mint",row).click(function(){
            //$bb.openPage("bch_vaccineInoculationHistory",{"info":data});
            var params = $bb.getPageParams();
            var busiTypeList = [params.busiType];
            $bb.openPage("bch_user",{"busiTypeList":busiTypeList,"userId":data.userId,"status":"readonly"});
        });*/
        $("td.operate .operate-box i.color-blue",row).unbind("click");
        $("td.operate .operate-box i.color-blue",row).click(function(){
            //$bb.openPage("bch_vaccineInoculationAdd",{"info":data});
            var params = $bb.getPageParams();
            var busiTypeList = [params.busiType];
            $bb.openPage("bch_user",{"busiTypeList":busiTypeList,"userId":data.userId});
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        $("#serchBtn").bind("click",function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/ptmanage/selectUserCpTaskList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});